package com.yami.shop.mystery.api.listener;

import cn.hutool.core.util.ObjectUtil;
import com.yami.shop.bean.event.ProdRightsEvent;
import com.yami.shop.bean.vo.ProdRightsVo;
import com.yami.shop.mystery.api.service.ActivityPresellManager;
import com.yami.shop.mystery.common.dto.ActivityPresellDto;
import com.yami.shop.mystery.common.enums.ActivityRuleEnum;
import com.yami.shop.mystery.common.service.ActivityPresellService;
import groovy.lang.Tuple;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.event.EventListener;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.List;
import java.util.Objects;

/**
 * 活动类权益通用校验类
 * @author wxf
 * @date 2023/4/18 10:36
 */
@Slf4j
@Component("prodActivityRightsListener")
@AllArgsConstructor
public class ProdActivityRightsListener {

    private final ActivityPresellService activityPresellService;

    private final ActivityPresellManager activityPresellManager;

    @EventListener(ProdRightsEvent.class)
    public void getProdRights(ProdRightsEvent event) {
        try {
            // 商品id为空、用户为空、商品活动id为0时，直接返回
            if (ObjectUtil.isEmpty(event.getProductSearchVO()) || ObjectUtil.isEmpty(event.getProductSearchVO().getProdId())
                    || ObjectUtil.isEmpty(event.getUserId()) || event.getProductSearchVO().getActivityId() == 0) {
                return;
            }

            // 通过商品id 查询商品关联的活动信息
            ActivityPresellDto presell = activityPresellService.detail(event.getProductSearchVO().getActivityId());
            if (Objects.isNull(presell)) {
                return;
            }
            event.getProductSearchVO().setRuleMaxNum(event.getProductSearchVO().getMaxNum());
            event.setRightsId(presell.getActivityRights()); // 活动关联的权益
            event.getProductSearchVO().setStartTime(presell.getPutawayTime()); // 开售时间

            presell.setSpuId(event.getProductSearchVO().getProdId());
            // 校验活动规则 1、白名单  2、规则  3、权益 ；第一位 是否可寄售  第二位 限购数  第三位 权益列表 第四位 规则的限购数； 二三四位 只有可寄售时返回值
            Tuple tuple = activityPresellManager.checkUser(presell, event.getUserId());
            if (!(boolean)tuple.get(0)) {
                return;
            }
            if (presell.getPreemptionPrice() != null && presell.getPreemptionPrice() > 0 && tuple.get(5).equals(ActivityRuleEnum.DISCOUNTS.getValue())) {
                BigDecimal price = new BigDecimal(String.valueOf(presell.getPreemptionPrice())).divide(new BigDecimal("100")).setScale(2);
                event.getProductSearchVO().setPrice(price.doubleValue()); //优惠价
            }
            if ((boolean)tuple.get(4)) {
                event.getProductSearchVO().setIsPurchase(1); // 是否可售卖
            }
            if (Integer.parseInt(tuple.get(1).toString()) != 0) { // 活动规则有限购数，给规则限购数
                event.getProductSearchVO().setMaxNum(Integer.parseInt(tuple.get(1).toString())); // 限购数
            }
            if (Integer.parseInt(tuple.get(3).toString()) != 0) { // 活动规则有限购数，给规则限购数
                event.getProductSearchVO().setRuleMaxNum(Integer.parseInt(tuple.get(3).toString())); // 规则限购数
            }

            event.setProdRights((List<ProdRightsVo>) tuple.get(2));

        } catch (Exception e) {
            log.error("获取该商品权益信息失败:", e);
        }
    }
}
